﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ELab.Serialization.Data;
using ELab.Communication.Messages.ControllerMessages;

namespace ELab.Controls
{
    public partial class ListInforControl : UserControl
    {
        public ListInforControl()
        {
            InitializeComponent();
        }

        public event EventHandler ViewOnlyItemRequested;
        public event EventHandler RemoteItemRequested;
        public event EventHandler StopViewItemRequested;
        public event EventHandler LockItemRequested;
        public event EventHandler UnlockItemRequested;
        public event EventHandler ControMeItemRequested;
        private GetUsersResponseMessage.UserResponse[] _Users = null;
        public GetUsersResponseMessage.UserResponse[] Users
        {
            get { return _Users; }
            set
            {
                _Users = value;
                if (_Users != null)
                {
                    this.Controls.Clear();
                    foreach (GetUsersResponseMessage.UserResponse user in _Users)
                    {
                        InforControl infor = new InforControl { CurrentUser = user, Name = user.ApplicationName, StudentsCount = user.IsOnline? "Online" : "Offline" };
                        infor.ViewOnlyRequested += infor_ViewOnlyRequested;
                        infor.RemoteRequested += infor_RemoteRequested;
                        infor.LockRequested += infor_LockRequested;
                        infor.Actived += infor_Actived;
                        infor.StopViewOrRemoteRequested += infor_StopViewOrRemoteRequested;
                        infor.UnlockRequested += infor_UnlockRequested;
                        infor.ControlMeRequested +=infor_ControlMeRequested;
                        infor.Dock = DockStyle.Top;
                        this.Controls.Add(infor);
                        Reload();
                    }
                }

            }
        } 

        public void Reload()
        {
            if (_Users != null)
            {
                foreach (GetUsersResponseMessage.UserResponse user in _Users)
                {
                    var control = this.Controls.Find(user.ApplicationName, true).FirstOrDefault() as InforControl;
                    if (control != null)
                    {
                        control.StudentsCount = (user.IsOnline ? "Online" : "Offline");
                    }
                }
            }
        }

        private void infor_RemoteRequested(object sender, EventArgs e)
        {
            if (RemoteItemRequested != null) RemoteItemRequested(sender, e);
        }

        private void infor_ViewOnlyRequested(object sender, EventArgs e)
        {
            if (ViewOnlyItemRequested != null) ViewOnlyItemRequested(sender, e);
        }

        private void infor_StopViewOrRemoteRequested(object sender, EventArgs e)
        {
            if (StopViewItemRequested != null) StopViewItemRequested(sender, e);
        }

        private void infor_LockRequested(object sender, EventArgs e)
        {
            if (LockItemRequested != null) LockItemRequested(sender, e);
        }

        private void infor_UnlockRequested(object sender, EventArgs e)
        {
            if (UnlockItemRequested != null) UnlockItemRequested(sender, e);
        }

        private void infor_ControlMeRequested(object sender, EventArgs e)
        {
            if (ControMeItemRequested != null) ControMeItemRequested(sender, e);
        }

        private InforControl oldActive = null;
        private void infor_Actived(object sender, EventArgs e)
        {
            var info = (InforControl)sender;
            info.IsActive = true;

            if (oldActive != null && oldActive != info) oldActive.IsActive = false;
            oldActive = info;
        }
    }
}
